<?php
namespace Admin\Controller;
use Admin\Model\MatchModel;
use Admin\Model\AreaModel;
use Admin\Model\StyleModel;
use \Common\Model\CommonModel;
use Think\Controller;

class MatchController extends BaseController{

    public function index(){
        //主页
        $matchModel=M('match');
        if(!empty($_GET['area_id'])){
            unset($_SESSION['like']['match']);
            $_SESSION['like']['match']=$_GET['area_id'];
            $condition1['name']=array('like',"%".$_GET['area_id']."%");
            $areas=M('area')->where($condition1)->select();
            $area_ids=array();
            foreach($areas as $key=>$value){
                $area_ids[]=$value['id'];
            }
            if(!empty($area_ids)){
                $condition['area_id']=array('in',$area_ids);
            }else{
                $this->display();
                exit;
            }
        }
        if($_GET['start']&&$_GET['end']){
            $condition['create']= array('between',array(strtotime($_GET['start']),strtotime($_GET['end'])));
        }
        $count = $matchModel->where($condition)->count();
        $page= new \Think\Page($count,1);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$matchModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();
        $list=CommonModel::status($list);
        $areaModel=new AreaModel();
        $list=$areaModel->getArea($list);
        $styleModel=new StyleModel();
        $list=$styleModel->getStyle($list);
        //var_dump($list);exit;
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }


    //修改页面
    public function update(){
        $matchModel=D('match');
        //$matchModel=new PriceModel();
        $da['id']=$_GET['id'];
        //找到原数据
        $list=$matchModel->finded($da);
        $areaModel=new AreaModel();
        $areas=$areaModel->getAll();
        $stylesModel=new StyleModel();
        $styles=$stylesModel->getAll();
        $this->assign('areas',$areas);
        $this->assign('styles',$styles);
        $this->assign('list',$list);
        $this->display();
    }
    //执行修改
    public function doUpdate(){
        $matchModel=D('match');
        $data['city']=$_POST['city'];
        $data['area_id']=$_POST['area_id'];
        $data['plot']=$_POST['plot'];
        $data['style_id']=$_POST['style_id'];
        $data['place']=$_POST['place'];
        $data['butget']=$_POST['butget'];
        $data['remark']=$_POST['remark'];
        $id=$_POST['id'];
        $where='id='.$id;
        // echo $id;exit;
        $list=$matchModel->finded($where);
        /* echo(json_encode($list));
         exit;*/
        $data['update']=time();
        //过滤重复内容
        $data=CommonModel::filter($data,$list);
        // echo json_encode($data);
        // exit;
        $res=$matchModel->update($where,$data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(2);
        }
    }

    //删除
    public function del()
    {
        $matchModel = D('match');
        $id = $_GET['id'];
        $res = $matchModel->deleted('id=' . $id);
        if ($res){
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }

    }
}